package com.java.mbti.dao.impl;

import java.io.File;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.springframework.stereotype.Component;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.java.mbti.dao.MbtiDAO;
import com.java.mbti.vo.MbtiTypeVO;
import com.java.mbti.vo.PaperDTO;

@Component
public class MbtiDAOImpl implements MbtiDAO{

	@Override
	public ArrayList<PaperDTO> mbtiTest() {
		ArrayList<PaperDTO> list = new ArrayList<PaperDTO>();
		try{
			String path = MbtiDAOImpl.class.getResource("").getPath();
			System.out.println("==] path : "+ path);
			
			File file = new File(path+"testpaper.xml");
			DocumentBuilderFactory docBuildFact = DocumentBuilderFactory.newInstance();
			DocumentBuilder docBuild = docBuildFact.newDocumentBuilder();
	
			Document doc = docBuild.parse(file);
			
			doc.getDocumentElement().normalize();

			NodeList personlist = doc.getElementsByTagName("person");
			
			PaperDTO dto = null;
			
			for(int i = 0; i < personlist.getLength(); i++){
				
				Node personNode = personlist.item(i);
				
				if(personNode.getNodeType() == Node.ELEMENT_NODE){
					Element personElmnt = (Element) personNode;
					
					NodeList noList = personElmnt.getElementsByTagName("no");
					Element noElmnt = (Element) noList.item(0);
					Node no_node = noElmnt.getFirstChild();
					String no = no_node.getNodeValue();
					
					NodeList choiceList = personElmnt.getElementsByTagName("choice");
					Element choiceElmnt = (Element) choiceList.item(0);
					Node choice_node = choiceElmnt.getFirstChild();
					String choice = choice_node.getNodeValue();
					
					NodeList choice1List = personElmnt.getElementsByTagName("choice1");
					Element choice1Elmnt = (Element) choice1List.item(0);
					Node choice1_node = choice1Elmnt.getFirstChild();
					String choice1 = choice1_node.getNodeValue();
					
					NodeList choice2List = personElmnt.getElementsByTagName("choice2");
					Element choice2Elmnt = (Element) choice2List.item(0); 
					Node choice2_node = choice2Elmnt.getFirstChild();
					String choice2 = choice2_node.getNodeValue();
					
					NodeList testList = personElmnt.getElementsByTagName("test");
					Element testElmnt = (Element) testList.item(0);
					Node test_node = testElmnt.getFirstChild();
					String test = test_node.getNodeValue();
					
					NodeList choice_a_pointList = personElmnt.getElementsByTagName("choice_a_point");
					Element choice_a_pointElmnt = (Element) choice_a_pointList.item(0);
					Node choice_a_point_node = choice_a_pointElmnt.getFirstChild();
					String choice_a_point = choice_a_point_node.getNodeValue();
					
					NodeList choice_b_point1List = personElmnt.getElementsByTagName("choice_b_point1");
					Element choice_b_point1Elmnt = (Element) choice_b_point1List.item(0);
					Node choice_b_point1_node = choice_b_point1Elmnt.getFirstChild();
					String choice_b_point1 = choice_b_point1_node.getNodeValue();

					NodeList choice_a_m_pointList = personElmnt.getElementsByTagName("choice_a_m_point");
					Element choice_a_m_pointElmnt = (Element) choice_a_m_pointList.item(0);
					Node choice_a_m_point_node = choice_a_m_pointElmnt.getFirstChild();
					String choice_a_m_point = choice_a_m_point_node.getNodeValue();
					
					NodeList choice_b_m_point1List = personElmnt.getElementsByTagName("choice_b_m_point1");
					Element choice_b_m_point1Elmnt = (Element) choice_b_m_point1List.item(0);
					Node choice_b_m_point1_node = choice_b_m_point1Elmnt.getFirstChild();
					String choice_b_m_point1 =choice_b_m_point1_node.getNodeValue();
					
					NodeList choice_a_f_pointList = personElmnt.getElementsByTagName("choice_a_f_point");
					Element choice_a_f_pointElmnt = (Element) choice_a_f_pointList.item(0);
					Node choice_a_f_point_node = choice_a_f_pointElmnt.getFirstChild();
					String choice_a_f_point = choice_a_f_point_node.getNodeValue();
					
					NodeList choice_b_f_point1List = personElmnt.getElementsByTagName("choice_b_f_point1");
					Element choice_b_f_point1Elmnt = (Element) choice_b_f_point1List.item(0);
					Node choice_b_f_point1_node = choice_b_f_point1Elmnt.getFirstChild();
					String choice_b_f_point1 = choice_b_f_point1_node.getNodeValue();
					
					NodeList choice_c_point2List = personElmnt.getElementsByTagName("choice_c_point2");
					Element choice_c_point2Elmnt = (Element) choice_c_point2List.item(0);
					Node choice_c_point2_node = choice_c_point2Elmnt.getFirstChild();
					String choice_c_point2 = choice_c_point2_node.getNodeValue();
					
					NodeList papertype1List = personElmnt.getElementsByTagName("papertype1");
					Element papertype1Elmnt = (Element) papertype1List.item(0);
					Node papertype1_node = papertype1Elmnt.getFirstChild();
					String papertype1 = papertype1_node.getNodeValue();
					
					NodeList papertype2List = personElmnt.getElementsByTagName("papertype2");
					Element papertype2Elmnt = (Element) papertype2List.item(0);
					Node papertype2_node = papertype2Elmnt.getFirstChild();
					String papertype2 = papertype2_node.getNodeValue();
					
					dto = new PaperDTO(no,choice,choice1,choice2,test,choice_a_point,choice_b_point1,
							choice_a_m_point, choice_b_m_point1, choice_a_f_point, choice_b_f_point1,
							choice_c_point2, papertype1, papertype2);
					
					list.add(dto);
					
				} 
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public MbtiTypeVO searchType(String type) {
		MbtiTypeVO dto = null;
		try{
			String path = MbtiDAOImpl.class.getResource("").getPath();
			
			File file = new File(path+"type.xml");
			DocumentBuilderFactory docBuildFact = DocumentBuilderFactory.newInstance();
			DocumentBuilder docBuild = docBuildFact.newDocumentBuilder();
	
			Document doc = docBuild.parse(file);
			
			doc.getDocumentElement().normalize();
			
			NodeList personlist = doc.getElementsByTagName("person");
			
			
			for(int i = 0; i < personlist.getLength(); i++){
				
				Node personNode = personlist.item(i);
				
				if(personNode.getNodeType() == Node.ELEMENT_NODE){
					Element personElmnt = (Element) personNode;
					
					NodeList type_nameList = personElmnt.getElementsByTagName("type_name");
					Element type_nameElmnt = (Element) type_nameList.item(0);
					Node type_name_node = type_nameElmnt.getFirstChild();
					String type_name = type_name_node.getNodeValue();
					
					if(type.equals(type_name)){
						NodeList type_noList = personElmnt.getElementsByTagName("type_no");
						Element type_noElmnt = (Element) type_noList.item(0);
						Node type_no_node = type_noElmnt.getFirstChild();
						String type_no = type_no_node.getNodeValue();
						
						
						
						NodeList type_titleList = personElmnt.getElementsByTagName("type_title");
						Element type_titleElmnt = (Element) type_titleList.item(0);
						Node type_title_node = type_titleElmnt.getFirstChild();
						String type_title = type_title_node.getNodeValue();
						
						NodeList type_descriptionList = personElmnt.getElementsByTagName("type_description");
						Element type_descriptionElmnt = (Element) type_descriptionList.item(0);
						Node type_description_node = type_descriptionElmnt.getFirstChild();
						String type_description = type_description_node.getNodeValue();
						
						NodeList type_kr_nmList = personElmnt.getElementsByTagName("type_kr_nm");
						Element type_kr_nmElmnt = (Element) type_kr_nmList.item(0);
						Node type_kr_nm_node = type_kr_nmElmnt.getFirstChild();
						String type_kr_nm = type_kr_nm_node.getNodeValue();
						
						
						dto = new MbtiTypeVO(type_no,type_name,type_title,type_description,type_kr_nm);
						
						break;
					}
				} 
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return dto;
	}

}